from django.db import models

from apps.account.models import User
from apps.course.models import Course
from libs.mixins import ModelMixin
from public.constants import hds


class Order(models.Model, ModelMixin):
    PAYMENT_TYPE_CHOICES = (
        ('wechat', '微信支付'),
        ('offline', '线下支付'),
    )

    ORDER_STATUS_CHOICES = (
        ('pending', '待支付'),
        ('paid', '已支付'),
        ('cancelled', '已取消'),
    )

    order_no = models.CharField(max_length=32, unique=True, verbose_name='订单编号')
    payment_type = models.CharField(max_length=10, choices=PAYMENT_TYPE_CHOICES, verbose_name='支付方式')
    payment_code = models.CharField(max_length=32, null=True, blank=True, verbose_name='付款备注')

    # 学生信息
    student_name = models.CharField(max_length=32, verbose_name='学生姓名')
    student_id_card = models.CharField(max_length=18, verbose_name='身份证号')
    parent_name = models.CharField(max_length=32, verbose_name='家长姓名')
    parent_phone = models.CharField(max_length=11, verbose_name='家长手机')
    student_type = models.CharField(max_length=32, verbose_name='学员类型')
    grade = models.CharField(max_length=32, verbose_name='年级', null=True, blank=True)
    class_number = models.CharField(max_length=32, verbose_name='班级', null=True, blank=True)
    class_type = models.CharField(max_length=32, verbose_name='上课类型', null=True, blank=True)

    # 学校信息
    school_name = models.CharField(max_length=64, verbose_name='学校名称')
    exam_year = models.CharField(max_length=4, verbose_name='考试年份')

    openid = models.CharField(max_length=100, unique=True, null=True)  # 微信openid
    session_key = models.CharField(max_length=100, null=True)  # session_key
    nickname = models.CharField(max_length=100, null=True)  # 昵称

    # 订单信息
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='订单金额')
    status = models.CharField(max_length=10, choices=ORDER_STATUS_CHOICES, default='pending', verbose_name='订单状态')

    # 支付信息
    paid_time = models.DateTimeField(null=True, blank=True, verbose_name='支付时间')
    transaction_id = models.CharField(max_length=32, null=True, blank=True, verbose_name='微信支付交易号')

    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    @property
    def encoded_id(self):
        return hds.encode_id(self.id)

    @classmethod
    def decode_id(cls, encoded_id):
        return hds.decode_id(encoded_id)

    class Meta:
        db_table = 'order'
        ordering = ('-id',)
        verbose_name = '订单'
        verbose_name_plural = verbose_name


class OrderCourse(models.Model, ModelMixin):
    order = models.ForeignKey(Order, on_delete=models.CASCADE, related_name='courses', verbose_name='订单')
    course_id = models.IntegerField(verbose_name='课程ID')
    course_name = models.CharField(max_length=64, verbose_name='课程名称')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='课程价格')

    class Meta:
        db_table = 'order_course'
        ordering = ('-id',)
        verbose_name = '订单课程'
        verbose_name_plural = verbose_name
